package com.talkweb.ncfw.web.action;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import com.talkweb.ncfw.component.dict.entity.DictEntry;
import com.talkweb.ncfw.component.dict.entity.DictType;
import com.talkweb.ncfw.component.dict.service.IDictService;
import com.talkweb.ncfw.data.IDataMap;
import com.talkweb.ncfw.data.Page;
import com.talkweb.ncfw.data.ProcessResult;
import com.talkweb.ncfw.exceptions.BizBaseException;
import com.talkweb.ncfw.exceptions.ViewBaseException;
import com.talkweb.ncfw.utils.StringUtils;
import com.talkweb.ncfw.web.struts2.ExtGridBaseAction;
import com.talkweb.ncfw.web.utils.HttpServletUtils;
import com.talkweb.ncfw.web.utils.tree.TreeBean;
import com.talkweb.ncfw.web.utils.tree.TreeUtils;

/**
 * <p>文件名称: DictAction.java</p>
 * <p>文件描述: 字典管理</p>
 * <p>版权所有: 版权所有(C)2010</p>
 * <p>内容摘要: 包含字典类型与字典项管理</p>
 * <p>其他说明: 其它内容的说明</p>
 * <p>完成日期: 2011-9-26</p>
 * <p>修改记录1:</p>
 * <pre>
 *    修改日期:
 *    修 改 人:
 *    修改内容:
 * </pre>
 * <p>修改记录2：…</p>
 * @author  Wuqingming
 */
public class DictAction extends ExtGridBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8514860047916624270L;
	
	private IDictService dictService;
	
	public IDictService getDictService() {
		return dictService;
	}

	public void setDictService(IDictService dictService) {
		this.dictService = dictService;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-26
	 * @Description：录入字典类型信息
	 * @return
	 * @throws ViewBaseException
	 */
	public String inputDictType () throws ViewBaseException {
		boolean flag = false;
		String msg = null;
		int returnCode = 0;
		Map<String, Object> paraMap = this.getParameterMap();
		DictType dictType = new DictType();
		try {
			BeanUtils.copyProperties(dictType, paraMap);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		String optype = this.getParameter("optype");
		if (StringUtils.equals(optype, "add")) {//新增字典类型
			try {
				returnCode = this.dictService.addDictType(dictType);
			} catch (BizBaseException e) {
				throw new ViewBaseException(e.getMessage(), e);
			}
			flag = (returnCode == 1);
			msg = flag ? "字典类型添加成功" : "字典类型添加失败";
		} else {//修改字典类型
			try {
				returnCode = this.dictService.updateDictType(dictType);
			} catch (BizBaseException e) {
				throw new ViewBaseException(e.getMessage(), e);
			}
			flag = (returnCode == 1);
			msg = flag ? "字典类型信息修改成功" : "字典类型信息修改失败";
		}
		ProcessResult result = new ProcessResult(flag, msg);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-26
	 * @Description：删除字典类型
	 * @return
	 * @throws ViewBaseException
	 */
	public String deleteDictType () throws ViewBaseException {
		boolean flag = false;
		String msg = null;
		int returnCode = 0;
		String dicttypeIds = this.getParameter("dicttypeIds");
		String[] dicttypeIdArray = null;
		if (!StringUtils.isEmpty(dicttypeIds)) {
			dicttypeIdArray = dicttypeIds.split(",");
		}
		if (dicttypeIdArray == null || dicttypeIdArray.length == 0 || dicttypeIdArray[0].trim().length() == 0) {
			ProcessResult result = new ProcessResult(false, "字典类型编号不能为空.");
			if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
				HttpServletUtils.outJson(this.getResponse(), result);
			}
		}
		try {
			returnCode = this.dictService.deleteDictTypeBatch(dicttypeIdArray);
			flag = (returnCode == 1);
			msg = flag ? "字典类型删除成功" : "字典类型删除失败";
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		ProcessResult result = new ProcessResult(flag, msg);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}

	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-26
	 * @Description：获取单个字典类型的详细信息
	 * @return
	 * @throws ViewBaseException
	 */
	public String getDicttypeByDictypeId () throws ViewBaseException {
		String dicttypeId = this.getParameter("dicttypeId");
		DictType dictType = null;
		try {
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("dicttypeId", dicttypeId);
			List<DictType> resultList = this.dictService.findDictTypeByCondition(condition);
			dictType = (resultList == null || resultList.size() == 0) ? null : resultList.get(0);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), dictType);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-26
	 * @Description：查询字典类型树
	 * @return
	 * @throws ViewBaseException
	 */
	public String findDictTypeSupportTree () throws ViewBaseException {
		IDataMap<String, Object> condition = this.getParameterMap(); 
		List<DictType> dictTypeList = null;
		try {
			dictTypeList = this.dictService.findDictTypeByCondition(condition);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		if (dictTypeList == null || dictTypeList.size() == 0) {
			return null;
		}
		List<TreeBean> treeBeanList = new ArrayList<TreeBean>();
		for (DictType dictType : dictTypeList) {
			TreeBean treeBean = new TreeBean();
			treeBean.setId(dictType.getDicttypeId());
			treeBean.setParentid(dictType.getParentId());
			treeBean.setText(dictType.getDicttypeName());
			treeBeanList.add(treeBean);
		}
		List<TreeBean> treeList = TreeUtils.onTree(treeBeanList, "", "");
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJsonArray(this.getResponse(), treeList);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-4-6
	 * @Description：列出字典类型列表
	 * @return
	 */
	public String listDictType () throws ViewBaseException {
		Page<DictType> page = this.getPage();
		IDataMap<String, Object> condition = this.getParameterMap(); 
		try {
			page = this.dictService.findDictType(page, condition);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			this.bindDataGrid(page);
		}
		return null;
	}


	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-26
	 * @Description：获取字典项列表
	 * @return
	 * @throws ViewBaseException
	 */
	public String listDictEntry () throws ViewBaseException {
		Page<DictEntry> page = this.getPage();
		IDataMap<String, Object> condition = this.getParameterMap(); 
		try {
			page = this.dictService.findDictEntry(page, condition);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			this.bindDataGrid(page);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-26
	 * @Description：录入字典项信息
	 * @return
	 * @throws ViewBaseException
	 */
	public String inputDictEntry () throws ViewBaseException {
		boolean flag = false;
		String msg = null;
		int returnCode = 0;
		Map<String, Object> paraMap = this.getParameterMap();
		DictEntry dictEntry = new DictEntry();
		try {
			BeanUtils.copyProperties(dictEntry, paraMap);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		String optype = this.getParameter("optype");
		if (StringUtils.equals(optype, "add")) {//新增字典项
			try {
				returnCode = this.dictService.addDictEntry(dictEntry);
			} catch (BizBaseException e) {
				throw new ViewBaseException(e.getMessage(), e);
			}
			flag = (returnCode == 1);
			msg = flag ? "字典项添加成功" : "字典项添加失败";
		} else {//修改字典项
			try {
				returnCode = this.dictService.updateDictEntry(dictEntry);
			} catch (BizBaseException e) {
				throw new ViewBaseException(e.getMessage(), e);
			}
			flag = (returnCode == 1);
			msg = flag ? "字典项信息修改成功" : "字典项信息修改失败";
		}
		ProcessResult result = new ProcessResult(flag, msg, returnCode);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-26
	 * @Description：删除字典类型
	 * @return
	 * @throws ViewBaseException
	 */
	@SuppressWarnings("unchecked")
	public String deleteDictEntry () throws ViewBaseException {
		boolean flag = false;
		String msg = null;
		int returnCode = 0;
		String pks = this.getParameter("pks");
		if (StringUtils.isEmpty(pks)) {
			ProcessResult result = new ProcessResult(false, "pks信息不能为空.", 0);
			if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
				HttpServletUtils.outJson(this.getResponse(), result);
			}
		}
		String[] pkArray = pks.split(";");
		Map<String, Object>[] pkMapArray = new Map[pkArray.length];
		for (int i = 0; i < pkArray.length; i++) {
			Map<String, Object> pkMap = new HashMap<String, Object>();
			String[] dicttypeIdAndDictIdArray = pkArray[i].split(",");
			pkMap.put("dicttypeId", dicttypeIdAndDictIdArray[0]);
			pkMap.put("dictId", dicttypeIdAndDictIdArray[1]);
			pkMapArray[i] = pkMap;
		}
		try {
			returnCode = this.dictService.deleteDictEntryBatch(pkMapArray);
			flag = (returnCode == 1);
			msg = flag ? "字典项删除成功" : "字典项删除失败";
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		ProcessResult result = new ProcessResult(flag, msg, returnCode);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}
	

	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-27
	 * @Description：获取字典项
	 * @return
	 * @throws ViewBaseException
	 */
	public String getDictEntry () throws ViewBaseException {
		String dicttypeId = this.getParameter("dicttypeId");	//字典类型ID
		String dictId = this.getParameter("dictId");			//字典项ID
		DictEntry dictEntry = null;
		try {
			dictEntry = this.dictService.getDictEntry(dicttypeId, dictId);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), dictEntry);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-26
	 * @Description：刷新字典缓存
	 * @return
	 * @throws ViewBaseException
	 */
	public String refreshDictCache () throws ViewBaseException {
		try {
			this.dictService.refreshDataDictCache();
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		ProcessResult result = new ProcessResult(true, "字典缓存刷新成功");
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}

}

